package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.entity.TreeMenu;
import com.example.mapper.TreeMenuMapper;
import com.example.service.TreeMenuService;

@Service
public class TreeMenuServiceImpl implements TreeMenuService{
	@Autowired
	private TreeMenuMapper mapper;
	
	/**
	 * 根据角色id集合查询对应的菜单
	 * 
	 * @return List
	 */
	@Override
	public List<TreeMenu> getModuleByRoleIds(String roleIds) {
		return mapper.getModuleByRoleIds(roleIds);
	}

	/*
	 * 生成菜单树
	 * 
	 * @return List
	 */
	@Override
	public List<TreeMenu> getTreeMenu(String roleIds) {
		List<TreeMenu> treeList = this.getModuleByRoleIds(roleIds);
		List<TreeMenu> TreeMenus = new ArrayList<TreeMenu>();
		for (TreeMenu TreeMenu : treeList) {
			if (TreeMenu.getParentid() == 0) {
				TreeMenus.add(findChildren(TreeMenu, treeList));
			}
		}
		return TreeMenus;
	}

	/**
	 * 查找子菜单
	 * 
	 * @return List
	 */
	@Override
	public TreeMenu findChildren(TreeMenu TreeMenu, List<TreeMenu> treeList) {
		for (TreeMenu item : treeList) {
			if (TreeMenu.getId() == item.getParentid()) {
				if (TreeMenu.getChildren() == null) {
					TreeMenu.setChildren(new ArrayList<TreeMenu>());
				}
				TreeMenu.getChildren().add(findChildren(item, treeList));
			}
		}
		return TreeMenu;
	}

	/*
	 * 生成菜单树
	 * 
	 * @return List
	 */
	@Override
	public List<TreeMenu> getTreeMenu2(String roleIds) {
		List<TreeMenu> treeList = this.getTreeMenu2(roleIds);
		List<TreeMenu> TreeMenus = new ArrayList<TreeMenu>();
		for (TreeMenu TreeMenu : treeList) {
			if (TreeMenu.getParentid() == 0) {
				TreeMenus.add(findChildren(TreeMenu, treeList));
			}
		}
		return TreeMenus;
	}

}
